package top.fengyulou.xiaofeng.controller;

import top.fengyulou.xiaofeng.common.Jr;
import top.fengyulou.xiaofeng.exception.BusinessException;
import top.fengyulou.xiaofeng.exception.ValidatorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ControllerExceptionHandler {

    private static final Logger LOG = LoggerFactory.getLogger(ControllerExceptionHandler.class);

    @ExceptionHandler(value = ValidatorException.class)
    @ResponseBody
    public Jr validatorExceptionHandler(ValidatorException e) {
        LOG.error(e.getMessage());
        return Jr.nom("请求参数错误");
    }

    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public Jr businessExceptionHandler(BusinessException e) {
        LOG.error("业务异常：{}", e.getCode().getDesc());
        return Jr.nom(e.getCode().getDesc());
    }
}
